<?php

if(isset($_POST['submit'])){
	$name = mysql_real_escape_string($_POST['name']);
	$description = mysql_real_escape_string($_POST['description']);
	$image = $_FILES['image'];
	$tags = mysql_real_escape_string($_POST['tags']);
		
	$problem = false;
	
	if(empty($m_username)){
		$problem = true;
		$content.= '
		<P class="error">Please login to search for items. Or, <a href="'.$addr.'/register.php">create an account</a>.</P>
		';
	}
	
	// Continue with checks if user is logged in
	if(!$problem){
		if(empty($name)){
			$problem = true;
			$content.= '
			<P class="error">Please enter a name for the item</P>
			';
		}
		
		if(empty($description)){
			$problem = true;
			$content .= '
			<P class="error">Please enter a small description of the item.</P>
			';
		}
		
		if(!empty($tags)){
			$tag_array = explode(',',$tags);
			$arr_length = count($tag_array);
			
			for($x = 0;$x < $arr_length; $x++){
				$tag_array[$x] = trim($tag_array[$x]);
			}
		}
		
		$find_item = mysql_query("SELECT * FROM items WHERE name='$name' AND description='$description' AND user_id='$m_user_id'");
		$item_exists = mysql_num_rows($find_item);
		
		if($item_exists){
			$problem = true;
			$content .= '
			<P class="error">You have already entered an item with the same name and description.</P>
			';
		}
	}
	if(!$problem){
	
		// Upload the picture
		$target_path;
		if(file_exists($image['tmp_name'])){
			$target_path = "images/";
			$image_name = basename($image['name']);
			$target_path .= $image_name;
			
			// Get width and height of original file
			list($width,$height) = getimagesize($image['tmp_name']);
			
			// if image is over 300px in one dimension
			if($width > 300 || $height > 300){
				$ext = substr($image_name, strrpos($image_name, '.') + 1);
				$ext = strtolower($ext);
				
				// if image is jpeg
				if($ext == 'jpg' || $ext == 'jpeg'){
					$tmp = imagecreatefromjpeg($image['tmp_name']);
					
				// if image is png
				}else if($ext == 'png'){
					$tmp = imagecreatefrompng($image['tmp_name']);
				
				// if image is gif
				}else if($ext == 'gif'){
					$tmp = imagecreatefromgif($image['tmp_name']);
				}
				
				// height is the largest dimension
				if($height > $width){
					$newheight = 300;
					$ratio = $width / $height;
					$newwidth = $ratio * 300;
					
				// width is the largest dimension
				}else{
					$newwidth = 300;
					$ratio = $height / $width;
					$newheight = $ratio * 300;
				}
				
				// blank image to copy on to
				$blank = imagecreatetruecolor($newwidth,$newheight);
				
				// Resize the image
				imagecopyresampled($blank,$tmp,0,0,0,0,$newwidth,$newheight,$width,$height); 
				
				// if image is jpeg
				if($ext == 'jpg' || $ext == 'jpeg'){
					imagejpeg($blank,$target_path,100);
					
				// if image is png
				}else if($ext == 'png'){
					imagepng($blank, $target_path,0);
					
				// if image is gif
				}else if($ext == 'gif'){
					imagegif($blank, $target_path);
				}
				
				// change mode
				chmod($target_path, 0764);
				
			// else image is less than 300px in any dimension
			}else{
				
				// move uploaded file
				if(move_uploaded_file($image['tmp_name'], $target_path)){
				
					// change mode
					chmod($target_path, 0764);
				}
			}
			
			$target_path = "images/";
			$image_name = basename($image['name']);
			$target_path .= $image_name;
			
			// Create a thumbify the image
			// if image is jpeg
			if($ext == 'jpg' || $ext == 'jpeg'){
				$tmp = imagecreatefromjpeg($target_path);
				
			// if image is png
			}else if($ext == 'png'){
				$tmp = imagecreatefrompng($target_path);
			
			// if image is gif
			}else if($ext == 'gif'){
				$tmp = imagecreatefromgif($target_path);
			}
			
			// Get width and height of original file
			list($width,$height) = getimagesize($target_path);
			
			// height is the largest dimension
			if($height > $width){
				$newheight = 50;
				$ratio = $width / $height;
				$newwidth = $ratio * 50;
				
			// width is the largest dimension
			}else{
				$newwidth = 50;
				$ratio = $height / $width;
				$newheight = $ratio * 50;
			}
			
			// blank image to copy on to
			$blank = imagecreatetruecolor($newwidth,$newheight);
			
			// Resize the image
			imagecopyresampled($blank,$tmp,0,0,0,0,$newwidth,$newheight,$width,$height);
			
			$ext = substr($image_name, strrpos($image_name, '.') + 1);
			$ext = strtolower($ext);
			
			// change target_path for new thumbnail image
			$target_path = 'images/thumb_'.$image_name;
			
			// if image is jpeg
			if($ext == 'jpg' || $ext == 'jpeg'){
				imagejpeg($blank,$target_path,100);
				
			// if image is png
			}else if($ext == 'png'){
				imagepng($blank, $target_path,0);
				
			// if image is gif
			}else if($ext == 'gif'){
				imagegif($blank, $target_path);
			}
			
			// change mode
			chmod($target_path, 0764);
		}
	
		// Insert item into item table
		$trash = mysql_num_rows(mysql_query("SELECT * FROM items"));
		$item_id = make_unique_id($name.$trash);
	
		$query = mysql_query("INSERT INTO items VALUES('','$item_id','$m_user_id','$m_member_type','$name','$description','$image_name')");
		
		$arr_size = count($tag_array);
		
		// Go through tag array and
		// add tags if they don't exist
		// also, create tagmap
		for($x = 0;$x < $arr_size; $x++){
			$tag_lower_name = strtolower($tag_array["$x"]);
			$tag_exists = mysql_num_rows(mysql_query("SELECT * FROM tags WHERE lower_name='$tag_lower_name'"));
			
			$tag_id = make_unique_id($tag_lower_name);
			
			// If tag is not in tag table,
			// insert it
			if(!$tag_exists){
				$query = mysql_query("INSERT INTO tags VALUES('$tag_id','$tag_lower_name','{$tag_array["$x"]}')");
			}
			
			$tag_map_exists = mysql_num_rows(mysql_query("SELECT * FROM tagmap WHERE tag_id='$tag_id' AND item_id='$item_id'"));
			
			// If tagmap is not the in the table
			// create tagmap for the new item
			if(!$tag_map_exists){
				$query = mysql_query("INSERT INTO tagmap VALUES('','$tag_id','$item_id')");
			}
		}
		
		mysql_close();
		
		// Refresh
		refresh('/index.php');
	}
}

?>
